﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace homework10
{
    /// <summary>
    /// WinSchoolEdit.xaml 的交互逻辑
    /// </summary>
    public partial class WinSchoolEdit : Window
    {
        public WinSchoolEdit()
        {
            InitializeComponent();
        }

        public School schoolToEdit { get; set; } = new School();
        public int action { get; set; } = 0;

        public bool AddSchool(School school)
        {
            try
            {
                using (var context = new AppDbContext())
                {
                    context.Schools.Add(school);
                    context.SaveChanges();
                }
                DbHelper.WriteLogs("Add School:" + school.ToString());
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public bool UpdateSchool(School school)
        {
            try
            {
                using (var context = new AppDbContext())
                {
                    context.Schools.Update(school);
                    context.SaveChanges();
                }
                DbHelper.WriteLogs("Update School:" + school.ToString());
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
           string strName = txtSchoolName.Text;
           strName = strName.Trim();
           if (strName != "")
               switch (action)
                {
                    case 1:
                        AddSchool(new School { Name = strName });
                        break;

                    case 2:
                        schoolToEdit.Name = strName;
                        UpdateSchool(schoolToEdit);
                        break;
                    default:break;
                }
            this.Close();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            switch (action)
            {
                case 1:
                    lblCaption.Content = "新增学校";
                    lblOldSchool.Visibility = Visibility.Collapsed;
                    txtOldSchool.Visibility = Visibility.Collapsed;
                    lblOldSchoolID.Visibility = Visibility.Collapsed;
                    txtOldSchoolID.Visibility = Visibility.Collapsed;
                    break;

                case 2:
                    lblCaption.Content = "修改校名";
                    lblOldSchoolID.Visibility = Visibility.Visible;
                    txtOldSchoolID.Visibility = Visibility.Visible;
                    lblOldSchool.Visibility = Visibility.Visible;
                    txtOldSchool.Visibility = Visibility.Visible;
                    txtOldSchoolID.Text = schoolToEdit.SchoolId.ToString();
                    txtOldSchoolID.IsReadOnly = true;
                    txtOldSchool.IsReadOnly = true;
                    txtOldSchool.Text = schoolToEdit.Name;

                    break;
                default: break;
            }
        }
    }
}
